﻿/*
 * 
 * Authors: Martin Paulík, Michal Zbraněk
 * 
 */ 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PB138___projekt
{
    /// <summary>
    /// Form for delete category. 
    /// </summary>
    public partial class deleteCategory : Form
    {
        XML xml = new XML();
        
        /// <summary>
        /// Create components of form and insert categories to comboBox.
        /// </summary>
        public deleteCategory()
        {
            InitializeComponent();
            insertToCombobox();
        }
        /// <summary>
        /// Insert all categories to comboBox in form deleteCategory.
        /// </summary>
        private void insertToCombobox()
        {
            List<string> categories = xml.getCategories();
            foreach (string category in categories)
                comboBox1.Items.Add(category);
        }

        /// <summary>
        /// Stops showing of errorProvider. 
        /// </summary>
        /// <param name="sender">Object</param>
        /// <param name="e">EventArgs</param>
        private void comboBox1_Validated(object sender, EventArgs e)
        {
            errorProvider1.SetError(comboBox1, "");
        }

        /// <summary>
        /// Checking if comboBox is valid and delete category. 
        /// </summary>
        /// <param name="sender">Object</param>
        /// <param name="e">CancelEventArgs</param>
        /// If text is empty in textBox1, then it will delete category and its dvds. 
        /// Otherwise name of category will be renamed on text in textBox1. 
        private void comboBox1_Validating(object sender, CancelEventArgs e)
        {
            if (comboBox1.Items.Count > 1)
            {
                if (comboBox1.Text.Length == 0)
                {
                    e.Cancel = true;
                    comboBox1.Select(0, comboBox1.Text.Length);
                    errorProvider1.SetError(comboBox1, "The name of category is required !!");
                }
                else
                {
                    if (xml.getCategory(comboBox1.Text) == null)
                    {
                        e.Cancel = true;
                        comboBox1.Select(0, comboBox1.Text.Length);
                        errorProvider1.SetError(comboBox1, "The name don't exist, you write other.");
                    }
                    else
                    {
                        if (textBox1.Text.Length != 0)
                            if (xml.getCategory(textBox1.Text) == null)
                                xml.deleteCategory(comboBox1.Text, textBox1.Text);
                            else
                                errorProvider1.SetError(textBox1, "The name already exists, you use the other.");
                        else
                            if (xml.deleteCategory(comboBox1.Text) == true)
                                MessageBox.Show("Delete done successfully", "SUCCESS", System.Windows.Forms.MessageBoxButtons.OK,
                           System.Windows.Forms.MessageBoxIcon.Information);
                            else
                                MessageBox.Show("Error. Delete have failed", "ERROR", System.Windows.Forms.MessageBoxButtons.OK,
                           System.Windows.Forms.MessageBoxIcon.Error);

                        reloadCombobox();
                        textBox1.Clear();
                    }

                }
            }
            else
            {
                MessageBox.Show("You can't delete last category, list have to have at least one category.", "ERROR", System.Windows.Forms.MessageBoxButtons.OK,
                           System.Windows.Forms.MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Calls validating of button. 
        /// </summary>
        /// <param name="sender">Object</param>
        /// <param name="e">EventArgs</param>
        private void button1_Click(object sender, EventArgs e)
        {
            comboBox1_Validating(new object(), new CancelEventArgs());
        }

        /// <summary>
        /// Refresh list of categories in comboBox.
        /// </summary>
        private void reloadCombobox()
        {
            comboBox1.Items.Clear();
            insertToCombobox();
        }

        /// <summary>
        /// Stops showing of errorProvider.
        /// </summary>
        /// <param name="sender">Object</param>
        /// <param name="e">EventArgs</param>
        private void textBox1_Validated(object sender, EventArgs e)
        {
            errorProvider1.SetError(textBox1, "");
        }
    }
}
